package com.mdc.mansion.api.web.common.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class SessionUtil {
	
	private static SessionUtil INSTANCE = new SessionUtil();

	private SessionUtil(){}

	/**
	 * 获取当前Request
	 * @return
	 */
	private HttpServletRequest getRequest() {  
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        return requestAttributes.getRequest();  
    } 
	
	/**
	 * 获取当前Session
	 * @return
	 */
	private HttpSession getSession() {  
        return getRequest().getSession(true);  
    }

    public static <T> T getAttribute(String key, Class<T> clazz){
		HttpSession session=INSTANCE.getSession();
		T ret = (T) session.getAttribute(key);
		return ret;
	}

	public static Object setAttribute(String key,Object o){
		HttpSession session=INSTANCE.getSession();
		session.setAttribute(key,o);
		return o;
	}
}
